package utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

import xrz.com.jr.R;
import xrz.com.jr.entity.SearchEntry;
import xrz.com.jr.utils.ImageUtils;
import xrz.com.jr.utils.ScreenUtil;

public class PopupShow {
    private static PopupWindow m_PopupWindow;
    private static ListView m_ListView;

    private static void initPopupWindow(Context context, int width) {
        if (width == 0)
            width = ScreenUtil.getInstant().getDisWidth(context) - 20;
        m_PopupWindow = new PopupWindow(context);
        m_PopupWindow.setFocusable(true);
        m_PopupWindow.setTouchable(true);
        m_PopupWindow.setOutsideTouchable(true);
        m_PopupWindow.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_dark_bg));
        m_PopupWindow.setWidth(width);
        m_PopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
    }

    private static void createListView(Context context, List<SearchEntry> list, OnItemClickListener listener) {
        m_ListView = new ListView(context);
        android.widget.AbsListView.LayoutParams layoutParams = new android.widget.AbsListView.LayoutParams(
                android.widget.AbsListView.LayoutParams.MATCH_PARENT,
                android.widget.AbsListView.LayoutParams.WRAP_CONTENT);
        m_ListView.setLayoutParams(layoutParams);
        m_ListView.setScrollingCacheEnabled(false);
        m_ListView.setOnItemClickListener(listener);
        MyAdapter adapter = new MyAdapter(list, context);
        m_ListView.setAdapter(adapter);
    }

    public static void dismiss() {
        if (m_PopupWindow != null && m_PopupWindow.isShowing())
            m_PopupWindow.dismiss();
    }

    public static void showPop(Context context, List<SearchEntry> list, View dropView, OnItemClickListener listener) {
        initPopupWindow(context, dropView.getWidth());
        createListView(context, list, listener);
        m_PopupWindow.setContentView(m_ListView);
        m_PopupWindow.showAsDropDown(dropView);
    }

    private static class MyAdapter extends BaseAdapter {

        private List<SearchEntry> list;
        private Context context;

        public MyAdapter(List<SearchEntry> list, Context context) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null)
                convertView = LayoutInflater.from(context).inflate(R.layout.item_pop_listview, null);
            TextView textView = (TextView) convertView.findViewById(R.id.item_name);
            ImageView image = (ImageView) convertView.findViewById(R.id.item_icon);
            textView.setText(list.get(position).getName());
            ImageUtils.loadimg(image, list.get(position).getIcon());
            return convertView;
        }

    }
}
